package com.ssh.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ssh.model.UserInfo;
import com.ssh.model.Users;

public class UserInfoDAO extends HibernateDaoSupport implements IUserInfoDAO{

	@Override
	public void saveUserInfo(UserInfo userInfo) {
		try {
			getHibernateTemplate().save(userInfo);
			
		} catch (RuntimeException re) {
		
			throw re;
		}
		
	}

	@Override
	public UserInfo findUserInfoById(long id) {
		
		try {
			UserInfo instance = (UserInfo) getHibernateTemplate().get(
					"com.ssh.model.UserInfo", id);
			return instance;
		} catch (RuntimeException re) {
			
			throw re;
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserInfo> findByProperty(String propertyName, Object value) {
		try {
			String queryString = "from UserInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			
			throw re;
		}
	}

	@Override
	public UserInfo findUserInfoByUserName(String username) {
		List<UserInfo> userInfo=findByProperty(USERNAME,username);
		if(userInfo==null||userInfo.size()==0){
			return null;
		}else{
			 return userInfo.get(0);
		}
	}

}
